package cc.bailongma.me.adapter;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cc.bailongma.me.R;
import cc.bailongma.me.entity.Parent;
import cc.bailongma.me.view.AddParentDialog.MyDialogListener;

public class ParentListAdapter extends BaseAdapter{
	private MyDialogListener listener;
	private Activity activity;
	private List<Parent> parents;
	public  int position=-1;
	public View view;
		public ParentListAdapter(FragmentActivity activity,List<Parent> parents,MyDialogListener listener) {
		this.parents=parents;
		this.listener=listener;
		this.activity=activity;
	}

		@Override
		public int getCount() {
			return parents.size();
		}

		@Override
		public Object getItem(int position) {
			return parents.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder vh=null;
			if(convertView==null){
				convertView=LayoutInflater.from(activity).inflate(R.layout.list_item_parent, null);
				vh=new ViewHolder();
				vh.tv_serial_number=(TextView) convertView.findViewById(R.id.tv_serial_number);
				vh.tv_chengwei=(TextView) convertView.findViewById(R.id.tv_chengwei);
				vh.tv_phone=(TextView) convertView.findViewById(R.id.tv_phone);
				vh.tv_delete=(TextView) convertView.findViewById(R.id.tv_delete);
				vh.tv_edit=(TextView) convertView.findViewById(R.id.tv_edit);
				convertView.setTag(vh);
			}
			
			final Parent p=parents.get(position);
			vh=(ViewHolder) convertView.getTag();
			vh.tv_serial_number.setText(p.getId()+"");
			vh.tv_chengwei.setText(p.getChengwei());
			vh.tv_phone.setText(p.getPhone());
			vh.tv_chengwei.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					showInfomation(p);
				}
			});
			vh.tv_phone.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					showInfomation(p);
				}
			});
			vh.tv_edit.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
				showEditDialog(p);
				}
			});
			vh.tv_delete.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
				showDeleteDialog(p);	
				}
			});
			return convertView;
		}


		class ViewHolder{
			TextView tv_serial_number;
			TextView tv_chengwei;
			TextView tv_phone;
			TextView tv_delete;
			TextView tv_edit;
		}

		protected void showDeleteDialog(final Parent parent) {
			View view=LayoutInflater.from(activity).inflate(R.layout.delete_parent_number_dialog, null);
			final AlertDialog delete_parent_number_dialog=new Builder(activity).setView(view).create();
			TextView tv=(TextView) view.findViewById(R.id.tv_parent_phone);
			tv.setText(parent.getChengwei()+"("+parent.getPhone()+")");
			view.findViewById(R.id.tv_ok).setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					parents.remove(parent);
					delete_parent_number_dialog.dismiss();
					listener.update();
				}
			});
			view.findViewById(R.id.tv_cancle).setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					delete_parent_number_dialog.dismiss();
				}
			});
			delete_parent_number_dialog.show();
		}
		
		protected void showEditDialog(final Parent parent) {
			View view=LayoutInflater.from(activity).inflate(R.layout.edit_parent_number_dialog, null);
			final AlertDialog edit_parent_number_dialog=new Builder(activity).setView(view).create();
			final EditText et_chengwei=(EditText) view.findViewById(R.id.et_chengwei);
			et_chengwei.setText(parent.getChengwei());
			final EditText et_phone=(EditText) view.findViewById(R.id.et_phone);
			et_phone.setText(parent.getPhone());
			view.findViewById(R.id.tv_submit).setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					parent.setChengwei(et_chengwei.getText().toString());
					parent.setPhone(et_phone.getText().toString());
					Toast.makeText(activity, "修改完成", 0).show();
					edit_parent_number_dialog.dismiss();
					listener.update();
				}
			});
			view.findViewById(R.id.tv_cancle).setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					edit_parent_number_dialog.dismiss();
				}
			});
			edit_parent_number_dialog.show();
		}
		

		protected void showInfomation(Parent parent) {
		Toast.makeText(activity, "你点击了"+parent.getChengwei(), 0).show();
		}
		
}
